// 常量
import { CELLULAR_CLASSIFY_LIST, CELLULAR_PRIORITY_LIST, TOP_CHANNELS } from '@/constants';
import { parseToObject } from '@/utils';

import { CELLULAR_INFO_ROW_VALUE } from '../../constants';

// 工具类
import { message } from '@/utils/util';

// 请求接口
import { modifyCellularInfo } from './api';

export default {
  name: 'modify-cellularInfo-modal',
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    cellularRowData: String,
  },
  data() {
    return {
      cellularInfoRowValue: { ...CELLULAR_INFO_ROW_VALUE },
      formRules: {
        cellularCode: [
          {
            required: true,
            trigger: 'change',
            validator: (rule, value, callback) => {
              const reg = /^[0-9A-Z]+$/;
              const cellularCode = this.cellularInfoRowValue.cellularCode;
              if (!cellularCode) {
                callback('蜂窝编码不能为空');
              } else if (!reg.test(cellularCode)) {
                callback('蜂窝编码只能由大写字母和数字组成');
              } else {
                callback();
              }
            },
          },
        ],
        topValue: [
          {
            required: true,
            trigger: 'change',
            validator: (rule, value, callback) => {
              const reg = /^100$|^(\d|[1-9]\d)$/;
              const topValue = this.cellularInfoRowValue.topValue;
              if (!topValue) {
                callback('请输入...');
              } else if (!reg.test(topValue)) {
                callback('请输入大于等于0小于100的正整数');
              } else {
                callback();
              }
            },
          },
        ],
        cellularName: [
          {
            required: true,
            trigger: 'change',
            validator: (rule, value, callback) => {
              // const reg = /^[\u4e00-\u9fa5]+$/;
              const reg = /^[\u2E80-\u9FFF（）()-]+$/;
              const cellularName = this.cellularInfoRowValue.cellularName;
              if (!cellularName) {
                callback('蜂窝名称不能为空');
              } else if (!reg.test(cellularName)) {
                callback('请输入汉字');
              } else {
                callback();
              }
            },
          },
        ],
        cellularClassify: [
          {
            required: true,
            trigger: 'change',
            validator: (rule, value, callback) => {
              const cellularClassify = this.cellularInfoRowValue.cellularClassify;
              if (!cellularClassify) {
                callback('蜂窝类型不能为空');
              } else {
                callback();
              }
            },
          },
        ],
        cellularPriority: [
          {
            required: true,
            trigger: 'change',
            validator: (rule, value, callback) => {
              const reg = /[ABCD]/;
              const cellularPriority = this.cellularInfoRowValue.cellularPriority;
              if (!cellularPriority) {
                callback('调价优先级不能为空');
              } else if (!reg.test(cellularPriority)) {
                callback('请填写ABCD中任意一个等级');
              } else {
                callback();
              }
            },
          },
        ],
        autoPrice: [
          {
            required: true,
            trigger: 'change',
            validator: (rule, value, callback) => {
              if (!this.cellularInfoRowValue.autoPrice) {
                callback('是否自动化调价不能为空');
              } else {
                callback();
              }
            },
          },
        ],
      },
      formLabelWidth: '110px',
      cellularClassifyList: [...CELLULAR_CLASSIFY_LIST],
      cellularPriorityList: [...CELLULAR_PRIORITY_LIST],
      submiting: false,
    };
  },
  // watch: {
  //   cellularRowData(newValue) {
  //     if (!newValue) {
  //       return;
  //     }
  //     this.cellularInfoRowValue = {
  //       ...this.cellularInfoRowValue,
  //       ...parseToObject(newValue),
  //     };
  //   },
  // },

  methods: {
    onComfirm(name) {
      console.log(this.cellularInfoRowValue);
      this.$refs[name].validate(async valid => {
        if (valid) {
          try {
            let {
              id,
              topChannelCode,
              city,
              company,
              cellularCode,
              cellularName,
              cellularClassify,
              cellularPriority,
              autoPrice,
              topValue,
            } = this.cellularInfoRowValue;
            topChannelCode = TOP_CHANNELS.find(item => {
              if (item.name === topChannelCode) {
                return item;
              }
            }).id;
            topValue = Number(topValue);
            const cellular = {
              id,
              topChannelCode,
              city,
              company,
              cellularCode,
              cellularName,
              cellularClassify,
              cellularPriority,
              autoPrice,
              topValue,
            };
            this.submiting = true;
            await modifyCellularInfo(cellular);
            this.$emit('onComfirm');
            this.onClose();
            message('提示', '操作成功', 'success');
            this.submiting = false;
          } catch (error) {
            this.submiting = false;
            message('错误', error.message, 'error');
          }
        }
      });
    },
    onOpen() {
      this.cellularInfoRowValue = { ...parseToObject(this.cellularRowData) };
    },
    onCancel() {
      // this.cellularInfoRowValue = {};
      this.$refs.formRef.resetFields();
      this.$emit('onClose');
    },
    onClose() {
      this.onCancel();
    },
  },
};
